import { ref, unref } from 'vue'
import { adapter, request } from '@/request'
import { dashboardByGet } from '@/core/api/dashboard'
import type { Dayjs } from 'dayjs'
import type { ShowTypeEnum } from '@/views/dashboard/interface'
import type { PlatformEnum } from '@/core/enums/platform'
import type { RequestWrapper } from '@/core/interfaces/common'
import type { Order } from '@/core/interfaces/order'

export interface DashboardData {
  orderId?: Order['id']
  keyword?: string
  startTime?: Dayjs
  endTime?: Dayjs
  showType: ShowTypeEnum
  clientType?: PlatformEnum | -1
}

export interface DashboardParams extends Omit<DashboardData, 'startTime' | 'endTime'> {
  startTime?: string
  endTime?: string
  startRow: number
  rowCount: number
}

export interface DashboardTable {
  campaignId: number
  campaignName: string
  campaignNameStatus: string
  click: number
  cost: number
  date: string
  device: string
  impression: number
  provinceCityName: string
  userName: string
  keyword: string
}

export default function getDashboardTable() {
  const service = adapter<RequestWrapper<DashboardTable[]>>(request)
  const response = ref<DashboardTable[]>()
  const loading = ref(false)

  const fetch = (params: DashboardParams) => {
    const { url, method } = dashboardByGet()
    loading.value = true

    return service({
      config: {
        url,
        method,
        params: unref(params)
      },
      callback: (res) => {
        response.value = res.data.data
      },
      error: (err) => {
        console.log(err)
      },
      done: () => {
        loading.value = false
      }
    })
  }

  return [response, fetch, loading] as const
}
